package com.data.platform.core.utils;

import com.data.platform.core.enums.ResponseCode;
import com.data.platform.core.exception.BaseException;

import java.util.function.Supplier;

/**
 * 断言
 */
public class Assert {

    /**
     * 通用处理对外抛异常，用系统统一异常管理机制
     *
     * @param expression      true 正常，false 异常，需要对外抛出异常
     * @param supplier 异常响应码
     * @param message 异常信息
     */
    public static void isTrue(boolean expression, Supplier<ResponseCode> supplier, String message) {
        if (!expression) {
            throw new BaseException(nullSafeGet(supplier), message);
        }
    }

    private static ResponseCode nullSafeGet(Supplier<ResponseCode> supplier) {
        return supplier != null ? supplier.get() : ResponseCode.EXCEPTION;
    }
}
